package collection;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Stack;

/**
@Auther: MFM
@Date: 2021/7/23 - 07 - 23 - 15:34 
@version: 1.0
*/

public class StackTest {
    public static void main(String[] args) {
        Random random = new Random();
        List<Integer> list = new ArrayList<>();
        for (int j = 0; j < 10; j++) {
            list.add(random.nextInt(100));
        }

        Stack<Integer> stack = new Stack<>();
        Stack<Integer> midStack = new Stack<>();

        while (list.size()>0){
            int value = list.remove(list.size()-1);
            if (stack.size()>0){
                while (stack.size()>0){
                    Integer stackValue = stack.peek();
                    if (value>=stackValue){
                        break;
                    }else {
                        midStack.push(stack.pop());
                    }
                }
                stack.push(value);

                while (midStack.size()>0){
                    stack.push(midStack.pop());
                }
            }else {
                stack.push(value);
            }
        }
        System.out.println(stack);
    }
}
